package com.iss.yunpan.controller;

import com.github.pagehelper.PageHelper;
import com.iss.yunpan.entity.Users;
import com.iss.yunpan.service.IndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@SessionAttributes("users")
public class IndexController {

    @Autowired
    private IndexService service;

    @RequestMapping("login")
    public String login(Model model, String username, String password) {
        Users user = service.getUsersByUsernameAndPassword(username, password);
        if (username == null || "".equals(username) || password == null || "".equals(password)) {
            model.addAttribute("msg", "用户名或者密码不能为空");
            return "index";
        }
        if (user == null) {
            model.addAttribute("msg", "用户名或者密码不正确");
            return "index";
        }
        System.out.println(user.getRealName());
        model.addAttribute("users", user);
        return "main";
    }

    @RequestMapping("register")
    public String register() {
        return "register";
    }

    @RequestMapping("saveUser")
    public String saveUser(Model model, Users users) {
        boolean flag = service.insertUser(users);
        System.out.println(users);
        if (flag) {
            model.addAttribute("users", users);
            return "success";
        }
        model.addAttribute("msg", "注册失败，请检查所填内容是否符合规范");
        return "register";
    }

    @RequestMapping("checkUserByUserId")
    @ResponseBody
    public boolean checkUserByUserId(String userId) {
        if (!"".equals(userId)) {
            boolean flag = service.checkUserByUserId(userId);
            return flag;
        }
        return false;
    }
    @RequestMapping("fileAll")
    public String allFile(){
        return "file/fileList.html";
    }
    @RequestMapping("myShare")
    public String myShare(){return  "file/shareList";}
    @RequestMapping("myStar")
    public String myStar(){return "file/collectList";}
    @RequestMapping("deleteCon")
    public String deleteCon(){return "file/trashList";}
    @RequestMapping("logOut")
    public String logOut(HttpServletRequest request, SessionStatus sessionStatus){
        sessionStatus.setComplete();
        request.getSession().removeAttribute("users");
        return "redirect:/";
    }
    @RequestMapping("editPassword")
    public String editPassword(){
        return "user/password";
    }
    @RequestMapping("checkOldPasswordByPassword")
    @ResponseBody
    public boolean checkOldPasswordByPassword(HttpSession session,String oldPassword){
        System.out.println("旧密码"+oldPassword);
        if(oldPassword==null || oldPassword.equals("")){
            return false;
        }else {
            Users user=(Users)session.getAttribute("users");
            if(!user.getUserId().equals("")){
                Users users=service.getUsersByUsernameAndPassword(user.getUserId(),oldPassword);
                if(users==null){
                    return false;
                }else {
                    return true;
                }
            }
        }
        return false;
    }
    @ResponseBody
    @RequestMapping("modifyPassword")
    public boolean modifyPassword(HttpSession session,String newPassword){
        if(!newPassword.equals("")){
            Users user=(Users)session.getAttribute("users");
            boolean flag=service.modifyPassword(user.getUserId(),newPassword);
            return flag;
        }
        return false;
    }
}
